package factory.nvwa.lazyinitial;

import factory.general.Product;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author：LJ
 * @Description：延迟初始化工厂方法
 * @Date: 2017/11/26
 * @Modified By:
 */
public class ProductInitiaFactory {
    private static final Map<String, Product> prMap = new HashMap<String, Product>();

    public static synchronized Product createProduct(String className) {
        Product product = null;
        if (prMap.containsKey(className)) {
            product = prMap.get(className);
        } else {
            try {
                product = (Product) Class.forName(className).newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return product;
    }
}
